package wviz.index;



import java.util.TreeSet;

//import wviz.textmining.TwoWords;



//@Deprecated
public class TwoWordsSet extends TreeSet<TwoWords> {
    /**
     * this is just for test purposes, its usullay false and should be false except for test purposes...
     */
    private boolean debugMode = false;

    public void setDebugMode(boolean debugMode) {
        this.debugMode = debugMode;
    }

    /**
     * if the set contains an entry with word couple word1 and word2 then it is removed from the set and returns
     * if not, a new TwoWordsInfo2 which encapsulates word1 and word2 is made and returned
     *
     * @param word1 word1
     * @param word2 word2
     * @return extracted TwoWordsInfo2
     */
    public TwoWords extract(String word1, String word2) {
//        TwoWords refTwoWords = new TwoWords(word1, word2);

        TwoWords memberTwoWords = getSimilarTwoWordsInfo(word1, word2);
        if (memberTwoWords != null) { // same couple already exist in the set
            this.remove(memberTwoWords);
            return memberTwoWords;
        } else {
            return new TwoWords(word1, word2);
        }

    }

    /**
     * should be used in place of add()
     * In DEBUG_MODE this method guarantees that the pair of words does not
     * exist simultaniousely in any member of the set;
     * if so a Runtime exception is thrown
     *
     * @param twoWords the TwoWordsInfo2() object that should be added to the set
     */
    public void put(TwoWords twoWords) {
        if (debugMode) {
            if (getSimilarTwoWordsInfo(twoWords.getWord1(), twoWords.getWord2()) != null) {
                throw new RuntimeException("the entry: " + twoWords.toString() + " already exists in the set");
            }
        }
        add(twoWords);
    }

    // returns a member of the first member of the set that has same word couple (and posibley different connectivity value)
    private TwoWords getSimilarTwoWordsInfo(String w1, String w2) {
        for (TwoWords anyMember : this) {
            if (anyMember.getWord1().equals(w1) &&
                    anyMember.getWord2().equals(w2)) {
                return anyMember;
            }

        }

        return null;
    }

    public TreeSet<TwoWords> copyWhatContains(String word) {
        TreeSet<TwoWords> subSet = new TreeSet<TwoWords>();
        for (TwoWords member : this) {
            if (member.getWord1().equals(word) ||
                    member.getWord2().equals((word))) {
                subSet.add(member);
            }
        }
        return subSet;
    }
}
